#include <iostream>
#include <string>

#include "imgui.h"
#include "imgui_impl_sdl2.h"
#include "imgui_impl_opengl3.h"

#include "SDL2/SDL.h"

#include "glad/glad.h"

#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL_opengles2.h>
#else
#include <SDL_opengl.h>
#endif

#undef main

void ImGuiInfo()
{
    ImGui::Begin("Main Window");

    // 1. 标题栏高度
    float title_bar_height = ImGui::GetStyle().FramePadding.y * 2 + ImGui::GetFontSize();

    // 2. 整个窗口的尺寸和位置
    ImVec2 window_pos = ImGui::GetWindowPos();                    // 窗口位置
    ImVec2 window_size = ImGui::GetWindowSize();                 // 窗口大小

    // 3. 内容区域（不包括标题栏和边框）
    ImVec2 content_min = ImGui::GetWindowContentRegionMin();     // 内容区域的最小坐标
    ImVec2 content_max = ImGui::GetWindowContentRegionMax();     // 内容区域的最大坐标
    ImVec2 content_size = ImGui::GetContentRegionAvail();       // 可用内容区域大小

    // 4. 窗口边框大小
    float window_border_size = ImGui::GetStyle().WindowBorderSize;

    // 5. 窗口填充
    ImVec2 window_padding = ImGui::GetStyle().WindowPadding;

    // 显示这些信息
    ImGui::Text("Window Position: (%.1f, %.1f)", window_pos.x, window_pos.y);
    ImGui::Text("Window Size: (%.1f, %.1f)", window_size.x, window_size.y);
    ImGui::Text("Title Bar Height: %.1f", title_bar_height);
    ImGui::Text("Content Region Min: (%.1f, %.1f)", content_min.x, content_min.y);
    ImGui::Text("Content Region Max: (%.1f, %.1f)", content_max.x, content_max.y);
    ImGui::Text("Content Available: (%.1f, %.1f)", content_size.x, content_size.y);
    ImGui::Text("Border Size: %.1f", window_border_size);
    ImGui::Text("Window Padding: (%.1f, %.1f)", window_padding.x, window_padding.y);

    ImGui::End();
    
}

void ImGuiWindow()
{
    bool flag = true;
    if(flag) 
    {
        if(ImGui::Begin("Close Window and Child", &flag))
        {
            ImGui::BeginChild("Child", ImVec2(200, 200), ImGuiChildFlags_Border, ImGuiWindowFlags_MenuBar);
            ImGui::Text("Hello, World!");
            ImGui::EndChild();
            ImGui::End();
        }

        // Text 
        if(ImGui::Begin("Text"))
        {
            ImGui::Text("Text");
            ImGui::TextDisabled("Disabled text");

            ImGui::Text("haha%d", 123);

            ImGui::TextColored(ImVec4(255, 0, 0, 255), "red");
            ImGui::TextColored(ImVec4(0, 255, 0, 255), "blue");
            ImGui::TextColored(ImVec4(0, 0, 255, 255), "green");
            ImGui::TextColored(ImVec4(128, 128, 128, 255), "grey");

            ImGui::BulletText("aaa");
            ImGui::BulletText("bbb");
            ImGui::BulletText("ccc");

            ImGui::End();
        }

        // Button
        if(ImGui::Begin("Button"))
        {
            if(ImGui::Button("I am a Button")) 
            {
            }

            // 保存当前样式
            ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(0.0f, 0.6f, 0.6f));
            ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(0.0f, 0.7f, 0.7f));
            ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(0.0f, 0.8f, 0.8f));

            //使用修改后的样式绘制按钮
            ImGui::Button("Red");

            // // 恢复之前的样式
            ImGui::PopStyleColor(3); // 弹出之前推入的3个颜色设置

            // 箭头按钮
            ImGui::ArrowButton("##Left", ImGuiDir_Left);
            ImGui::ArrowButton("##Right", ImGuiDir_Right);

            ImGui::End();
        }

        // CheckBox
        if(ImGui::Begin("CheckBox"))
        {
            static bool flag = false;
            if(ImGui::Checkbox("FirstBox", &flag))
            {
            }

            if(flag)
                ImGui::Text("the box has been checked");

            ImGui::End();
        }

        //Ratio Button
        if(ImGui::Begin("Ratio Button"))
        {
            static int select = -1;
            ImGui::RadioButton("Zero", &select, 0); ImGui::SameLine();
            ImGui::RadioButton("One",  &select, 1); ImGui::SameLine();
            ImGui::RadioButton("Two",  &select, 2);

            if(select >= 0)
            {
                ImGui::Text("You Select %d\n", select);
            }

            ImGui::End();
        }

        // Slider
        if(ImGui::Begin("Slider"))
        {
            static float x = 0.f;
            static float y = 0.f;
            static int radius = 0;
            ImGui::SliderFloat("x",&x,0.f,1080.f);
            ImGui::SliderFloat("y",&y,0.f,720.f);
            ImGui::SliderInt("Size",&radius,0,100);

            ImGui::End();
        }
    }
}

void ImGuiMenuBar()
{
    bool no_titlebar       = false;
    bool no_scrollbar      = false;
    bool no_menu           = false;
    bool no_move           = false;
    bool no_resize         = false;
    bool no_collapse       = false;
    bool no_close          = false;
    bool no_nav            = false;
    bool no_background     = false;
    bool no_bring_to_front = false;
    bool unsaved_document  = false;

    bool open = true;
    bool* p_open = &open;

    ImGuiWindowFlags window_flags = 0;
    if (no_titlebar)        window_flags |= ImGuiWindowFlags_NoTitleBar;
    if (no_scrollbar)       window_flags |= ImGuiWindowFlags_NoScrollbar;
    if (!no_menu)           window_flags |= ImGuiWindowFlags_MenuBar;
    if (no_move)            window_flags |= ImGuiWindowFlags_NoMove;
    if (no_resize)          window_flags |= ImGuiWindowFlags_NoResize;
    if (no_collapse)        window_flags |= ImGuiWindowFlags_NoCollapse;
    if (no_nav)             window_flags |= ImGuiWindowFlags_NoNav;
    if (no_background)      window_flags |= ImGuiWindowFlags_NoBackground;
    if (no_bring_to_front)  window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus;
    if (unsaved_document)   window_flags |= ImGuiWindowFlags_UnsavedDocument;
    if (no_close)           p_open = NULL; // Don't pass our bool* to Begin

    if(ImGui::Begin("MenuBar", p_open, window_flags))
    {
        if (ImGui::BeginMenuBar())
        {
            if (ImGui::BeginMenu("File"))
            {
                if (ImGui::MenuItem("Quit", "Alt+F4")) 
                    std::cout << "Quit" << std::endl;

                ImGui::EndMenu();
            }
            if (ImGui::BeginMenu("Edit"))
            {
                if( ImGui::MenuItem("TODO") ){
                    //do something
                }
                ImGui::EndMenu();
            }
            ImGui::EndMenuBar();
        }

        ImGui::End();
    }

}


int main(int argc, char* argv[])
{
    // Setup SDL
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
    {
        printf("Error: %s\n", SDL_GetError());
        return -1;
    }

    const char* glsl_version = "#version 130";
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

#ifdef SDL_HINT_IME_SHOW_UI
    SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif


    // Create window with graphics context
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
    SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
    SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
    if (window == nullptr)
    {
        printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
        return -1;
    }

    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
    if (gl_context == nullptr)
    {
        printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
        return -1;
    }

    SDL_GL_MakeCurrent(window, gl_context);
    SDL_GL_SetSwapInterval(1); // Enable vsync

    gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);

    // Setup Dear ImGui context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;     // Enable Keyboard Controls
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls

    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsLight();

    // Setup Platform/Renderer backends
    ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
    ImGui_ImplOpenGL3_Init(glsl_version);

    ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

    bool done = false;
    while (!done)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            ImGui_ImplSDL2_ProcessEvent(&event);
            if (event.type == SDL_QUIT)
                done = true;
            if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
                done = true;
        }
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
        {
            SDL_Delay(10);
            continue;
        }

        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplSDL2_NewFrame();
        ImGui::NewFrame();
        {
             bool show_demo_window = true;
        //     // bool show_another_window = false;
        //     //if (show_demo_window)
            ImGui::ShowDemoWindow(&show_demo_window);
            ImGui::ShowDebugLogWindow(&show_demo_window);
        }

        {
            //ImGuiWindow();
            ImGuiMenuBar();

            ImGuiInfo();
        }

        ImGui::Render();
        glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);

        glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        SDL_GL_SwapWindow(window);
    }

    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplSDL2_Shutdown();
    ImGui::DestroyContext();

    SDL_GL_DeleteContext(gl_context);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}